﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Reflection;

namespace Reflection
{
    public class Reflection
    {

        public static TSource ReflectData<TSource>(DataTable table) where TSource : new()
        {
            TSource module = new TSource();
            try
            {
                Type moduletype = module.GetType();
                PropertyInfo[] moduleprams = moduletype.GetProperties();
                foreach (DataRow dr in table.Rows)
                {
                    foreach (PropertyInfo pram in moduleprams)
                    {
                        if (dr.Table.Columns.Contains(pram.Name))
                        {
                            object value = TrunData(pram.PropertyType.Name, dr[pram.Name].ToString());
                            pram.SetValue(module, value, null);
                        }
                    }
                }
            }
            catch (Exception e) { throw e; }
            return module;
        }

        public static TSource ReflectData<TSource>(DataTable table, TSource module) where TSource : new()
        {
            try
            {
                Type moduletype = module.GetType();
                PropertyInfo[] moduleprams = moduletype.GetProperties();
                foreach (DataRow dr in table.Rows)
                {
                    foreach (PropertyInfo pram in moduleprams)
                    {
                        if (dr.Table.Columns.Contains(pram.Name))
                        {
                            object value = TrunData(pram.PropertyType.Name, dr[pram.Name].ToString());
                            pram.SetValue(module, value, null);
                        }
                    }
                }
            }
            catch { }
            return module;
        }

        public static List<TSource> ReflectDataToList<TSource>(DataTable table) where TSource : new()
        {
            List<TSource> list = new List<TSource>();
            try
            {
                foreach (DataRow dr in table.Rows)
                {
                    TSource module = new TSource();
                    Type moduletype = module.GetType();
                    PropertyInfo[] moduleprams = moduletype.GetProperties();
                    foreach (PropertyInfo pram in moduleprams)
                    {
                        if (dr.Table.Columns.Contains(pram.Name))
                        {
                            object value = TrunData(pram.PropertyType.Name, dr[pram.Name].ToString());
                            pram.SetValue(module, value, null);
                        }
                    }
                    list.Add(module);
                }
            }
            catch { }
            return list;
        }

        public static List<TSource> ReflectDataToList<TSource>(DataTable table, List<TSource> list) where TSource : new()
        {
            try
            {
                foreach (DataRow dr in table.Rows)
                {
                    TSource module = new TSource();
                    Type moduletype = module.GetType();
                    PropertyInfo[] moduleprams = moduletype.GetProperties();
                    foreach (PropertyInfo pram in moduleprams)
                    {
                        if (dr.Table.Columns.Contains(pram.Name))
                        {
                            object value = TrunData(pram.PropertyType.Name, dr[pram.Name].ToString());
                            pram.SetValue(module, value, null);
                        }
                    }
                    list.Add(module);
                }
            }
            catch (Exception e) { throw e; }
            return list;
        }

        public static object TrunData(string datatype, object value)
        {
            switch (datatype)
            {
                case "Int32":
                    value = Int32.Parse(value.ToString());
                    break;
                case "String":
                    value = value.ToString();
                    break;
                case "Int64":
                    value = (DateTime.Parse(value.ToString()) - new DateTime(1970, 1, 1)).Ticks / 10000000;
                    break;
                case "Single":
                    value = float.Parse(value.ToString());
                    break;
            }
            return value;
        }
    }
}
